﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

namespace Icst.Leaflike.Web.controls
{
    [DefaultProperty("Text"), ParseChildren(true, "Items")]
    public class OptionSelector : WebControl
    {
        [DefaultValue("")]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        [MergableProperty(false)]
        public virtual ListItemCollection Items { get; private set; }
        [Category("Appearance"), DefaultValue("")]
        public string Group { get; set; }
        [Category("Appearance"), DefaultValue("")]
        public string Title { get; set; }
        [Category("Appearance"), DefaultValue("")]
        public string OnClientClick { get; set; }

        public OptionSelector()
        {
            Items = new ListItemCollection();
            OnClientClick = string.Empty;
        }

        public void AddItems(params ListItem[] items)
        {
            foreach (ListItem li in items)
            {
                RadioButton rad = new RadioButton() { Checked = li.Selected, GroupName = Group };
                rad.InputAttributes["value"] = li.Value;
                Controls.Add(rad);
            }
            Items.AddRange(items);
        }
        public void DisplayItemsOnly(params string[] values)
        {
            foreach (ListItem li in Items)
            {
                string value = li.Value;
                bool has = values.SingleOrDefault(p => p == value) != null;
                RadioButton control = Select(value);
                control.Visible = has;
                li.Enabled = has;
            }
        }
        public string Selected 
        { 
            get 
            {
                foreach (Control control in Controls)
                {
                    RadioButton rad = control as RadioButton;
                    if (control == null)
                        continue;
                    if (rad.Checked)
                        return rad.InputAttributes["value"];
                }
                return string.Empty;
            } 
        }
        public bool this[string v]
        {
            get 
            { 
                RadioButton rad = Select(v); 
                if (rad != null) 
                    return rad.Checked; 
                return false; 
            }
            set 
            { 
                RadioButton rad = Select(v); 
                if (rad != null) 
                    rad.Checked = value; 
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);

            writer.WriteLine("<div style=\"margin-top: 10px;margin-bottom: 10px\">");
            if (!string.IsNullOrEmpty(Title))
                writer.WriteLine(string.Format("<div class=\"optionbar\">{0}</div>", Title));

            writer.WriteLine("<table cellspacing=\"0\">");

            int count = Items.Count;
            writer.WriteLine("<tr>");
            for (int i = 0; i < count; ++i)
            {
                ListItem li = Items[i];
                if (li.Enabled)
                {
                    string link = li.Text;
                    writer.WriteLine(string.Format("<td><a href=\"{2}\" target=\"_blank\"><img width=\"{0}\" height=\"{1}\" src=\"{2}\"/></a></td>", "45", "45", link));
                }
            }
            writer.WriteLine("</tr>");

            writer.WriteLine("<tr>");
            for (int i = 0; i < count; ++i)
            {
                Control control = Controls[i];
                RadioButton rad = control as RadioButton;
                if (control == null)
                    continue;
                if (rad.Visible)
                {
                    writer.Write("<td align=\"center\">");
                    if (!string.IsNullOrEmpty(OnClientClick))
                    {
                        rad.InputAttributes["onclick"] = OnClientClick;
                        writer.Write("<input type=\"radio\" id=\"{3}\" {4} name=\"{0}\" value=\"{1}\" onclick=\"{2}\"/>", Group, rad.InputAttributes["value"], OnClientClick,rad.ClientID,rad.Checked?"checked=\"checked\"":"");
                    }
                    else
                    {
                        rad.RenderControl(writer);
                    }
                    writer.Write("</td>");
                }
            }
            writer.WriteLine("</tr>");

            writer.WriteLine("</table>");

            writer.WriteLine("</div>");
        }

        private RadioButton Select(string v)
        {
            foreach (Control control in Controls)
            {
                RadioButton rad = control as RadioButton;
                if (control == null)
                    continue;
                if (v == rad.InputAttributes["value"])
                    return rad;
            }
            return null;
        }
    }
}
